<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: JsCookMenuPortlet.php 466 2011-01-22 17:44:38Z steen.rabol $
 *
 */

Prado::using('Application.Common.Web.UI.WebControls.TJSCookMenu.TJSCookMenu');
Prado::using('Application.Portlets.Portlet');
Prado::using('Application.Portlets.JsCookMenuPortlet.Common.JSCookMenuRecord');
Prado::using('Application.Common.Data.PortalSiteMenuRecord');
Prado::using('Application.Common.Data.PortalSiteMenuItemRecord');

class JsCookMenuPortlet extends Portlet
{
	private	$_jsmenuRecord;

	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->_jsmenuRecord	= JSCookMenuRecord::finder()->findByid($this->Parameters['id']);

		if($this->_jsmenuRecord)
		{
			$jsmenu	= new TJSCookMenu();
			$jsmenu->Id						= 'jscookmenu_' . $this->_jsmenuRecord->id;
			$jsmenu->ThemeName				= $this->_jsmenuRecord->theme;
			$jsmenu->MenuOrientation		= $this->_jsmenuRecord->menuorientation;

			$criteria = new TActiveRecordCriteria;
			$criteria->Condition = 'smid = :smid and parent=:parent and active=:active';
			$criteria->Parameters[':smid']		= $this->_jsmenuRecord->sitemenuid;
			$criteria->Parameters[':parent']	= 0;
			$criteria->Parameters[':active']	= 1;
			$criteria->OrdersBy['displayorder'] = 'asc';
			$menitemspages					= PortalSiteMenuItemRecord::finder()->findAll($criteria);

			if($menitemspages)
			{
				$i	= time();

				foreach($menitemspages as $mipage)
				{
					if(!$this->User->IsGuest && TPropertyValue::ensureInteger($mipage->showafterlogin) === 0)
						continue;

					if($this->User->IsGuest && TPropertyValue::ensureInteger($mipage->needlogin) === 1)
						continue;

					$item = new TJSCookMenuItem;
					$item->Title	= Prado::localize($mipage->title);
					$item->Id		= 'mi_' . $i;
					$item->Visible	= true;
					$item->Url		= $mipage->page != '' ? $this->Service->constructUrl($mipage->page) : '';
					$su				= $this->AddSubItems($item,$this->_jsmenuRecord->sitemenuid,$mipage->id);

					if($su !== null)
						$item->Controls[] = $su;

					$jsmenu->Controls[]	= $item;

					$i++;
				}
			}
			$this->JsCookMenuPlaceholder->Controls[] = $jsmenu;
		}
	}

	public function AddSubItems(&$pitem,$mid, $pid)
	{
		$criteria = new TActiveRecordCriteria;
		$criteria->Condition = 'smid = :smid and parent=:parent and active=:active';
		$criteria->Parameters[':smid']		= $mid;
		$criteria->Parameters[':parent']	= $pid;
		$criteria->Parameters[':active']	= 1;
		$criteria->OrdersBy['displayorder'] = 'asc';
		$menitemspages					= PortalSiteMenuItemRecord::finder()->findAll($criteria);

		if($menitemspages)
		{
			$i = time();
			$item = null;
			foreach($menitemspages as $mipage)
			{
				if(!$this->User->IsGuest && TPropertyValue::ensureInteger($mipage->showafterlogin) === 0)
					continue;

				if($this->User->IsGuest && TPropertyValue::ensureInteger($mipage->needlogin) === 1)
					continue;

				$item 				= new TJSCookMenuItem;
				$item->Title		= Prado::localize($mipage->title);
				$item->Id			= 'mi_' . $i;
				$item->Visible		= true;
				$item->Url			= $mipage->page !== '' ? $this->Service->constructUrl($mipage->page) : '';
				$su = $this->AddSubItems($item,$this->_jsmenuRecord->sitemenuid,$mipage->id);

				if($su !== null)
					$item->Controls[] = $su;

				$pitem->Controls[]	= $item;

				$i++;
			}

			return $item;
		}

		return null;
	}
}
?>